
package org.japril.swing.jutil.xmloperations;

import javax.swing.table.AbstractTableModel;

import org.japril.jutil.xmloperations.ProtoXmlOperation;
import org.japril.jutil.xmloperations.XmlOperation;
import org.japril.jutil.xmloperations.XmlOperations;

@SuppressWarnings("serial")
public final class XmlOperationsTableModel extends AbstractTableModel 
{
  private final XmlOperations m_operations;
  
  /**
   * <p>
   *   Creates empty model.
   * </p>
   * <p>
   *   Use 
   *   {@link #getOperations()} to get adapted.
   * </p>
   */
  public XmlOperationsTableModel(){ this(null); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param ops {@link XmlOperations}. If {@code null} an empty
   *            {@link XmlOperations} is created, use
   *            {@link #getOperations()} to fetch created.
   */
  public XmlOperationsTableModel(XmlOperations ops)
  { 
    m_operations=(ops==null) ? new XmlOperations() : ops; 
  }
  
  /**
   * <p>
   *   Gets adapted 
   *   {@link XmlOperations}.
   * </p>
   * @return Adapted.
   */
  public XmlOperations getOperations(){ return m_operations; }
  
  /**
   * <p>
   *   Gets 
   *   {@link XmlOperation}.
   * </p>
   * @param idx Index of operation to get.
   * @return {@code XmlOperation}.
   * @throws IndexOutOfBoundsException If {@code idx<0 || idx>=getRowCount()}.
   */
  public XmlOperation getOperation(int idx)
  {
    if (m_operations==null)
    {
      throw new IndexOutOfBoundsException(Integer.toString(idx));
    }
    
    return m_operations.getOperation(idx);
  }

  // Specified in TableModel.
  public int getColumnCount(){ return 1; }

  // Specified in TableModel.
  public int getRowCount(){ return m_operations.size(); }

  // Specified in TableModel.
  public Object getValueAt(int row, int col)
  {
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    XmlOperation o=m_operations.getOperation(row);
    return o.getName();
  }

  @Override
  public Class<?> getColumnClass(int col)
  {
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    return String.class;
  }

  @Override
  public String getColumnName(int col)
  {
    if (col!=0)
    {
      throw new IndexOutOfBoundsException(Integer.toString(col));
    }
    
    return "Name";
  }
  
  /**
   * <p>
   *   Creates 
   *   {@link ProtoXmlOperation} and adds it to table.
   * </p>
   * @param name Name on operation.
   * @return Created.
   */
  public ProtoXmlOperation createProto(String name)
  {
    ProtoXmlOperation retVal=new ProtoXmlOperation(name);
    int size=m_operations.size();
    m_operations.addOperation(retVal);
    fireTableRowsInserted(size, size);
    return retVal;
  }
  
  /**
   * <p>
   *   Removes operation.
   * </p>
   * @param idx Index of operation to remove.
   * @throws IndexOutOfBoundsException If {@code idx<0 || idx>=getRowCount()}.
   */
  public void remove(int idx)
  {
    m_operations.removeOperation(idx);
    fireTableRowsDeleted(idx, idx);
  }
 
}
